package com.itheima.user.controller;

import com.itheima.order.pojo.Order;
import com.itheima.user.feign.OrderFeign;
import com.itheima.user.pojo.User;
import com.itheima.user.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    OrderFeign orderFeign;

    @Value("${itheima}")
    private String itheima;

    /**
     * 根据用户名查询订单和用户信息
     * @param username
     * @return
     */
    @GetMapping("/{username}")
    public Map findUserByUserName(@PathVariable("username") String username){
        if (StringUtils.isEmpty(username)){
            return null;
        }
        System.out.println(itheima);
        User user = userService.findUserByUserName(username);
        List<Order> orders = orderFeign.findOrdersByUserName(user.getUsername());
        Map<String,Object> map = new HashMap<>();
        map.put("user",user);
        map.put("orders",orders);
        return map;
    }
}
